/*
 * Copyright 2016 Wealth Inc. All rights reserved.
 */
package com.wealth.member.service.impl;

import iaas.enums.StatusEnum;
import iaas.service.impl.BaseServiceImpl;
import iaas.utils.ResultObject;

import java.util.Date;

import org.hibernate.service.spi.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.wealth.member.entity.Member;
import com.wealth.member.entity.Membership;
import com.wealth.member.repository.MemberDao;
import com.wealth.member.repository.MembershipDao;
import com.wealth.member.service.IMemberService;
import com.wealth.system.entity.Corporation;

/**
 * @author chan
 */
@Component
public class MemberServiceImpl extends BaseServiceImpl<Member> implements
		IMemberService {

	@Autowired
	private MemberDao memberDao;
	@Autowired
	private MembershipDao membershipDao;

 
	 /** 绑定手机号码 */
	 public ResultObject bindTelephone(Long memberid,Long corpid,String telephone){
		 //暂时不考虑重复手机号
		 Member m = memberDao.findByUsername(telephone);
		 Membership mship =  membershipDao.findByMemberIdAndCorpId(m!=null?m.getId():0, corpid);
		 //手机号码已经绑定
		 if(m!=null && mship!=null && mship.getOpenid()!=null){
			 return new ResultObject("520", "该手机号已绑定过", telephone);
		 } 
		 
		 
		 Membership membership =  membershipDao.findByMemberIdAndCorpId(memberid, corpid);
		 //PC端已经注册该手机号码,和微信端合并,微信端注册的记录copy到PC端注册的记录
		 if(m!=null && m.getUsername().equals(telephone)){
			
			 Member member = membership.getMember();
			 
			 m.setHeadimgurl(member.getHeadimgurl());
			 m.setNickname(member.getNickname());
			 m.setSex(member.getSex());
			 
			 memberDao.save(m);   //微信端移动到PC端那条数据 
			
			 mship.setOpenid(membership.getOpenid());  //微信端membership移动到PC端
			 membershipDao.save(mship);
			 
			 membership.setCorp(null);//置空微信端membership 不删除
			 membership.setOpenid(null);
			 membershipDao.save(membership);
			 
			 //TODO 购物车数据未合并
			 
			 return  new ResultObject("000", "绑定成功", telephone);
			 
		 }else{
			 //未绑定手机号码 
			 Member member = membership.getMember();
			 member.setUsername(telephone);
			 memberDao.save(member);  
			 return  new ResultObject("000", "绑定成功", telephone);
		 } 
 
		 
	 }
	  
	 
	 /** 注册 */
	 public Membership reg(String openid,String headimgurl,String nickname,Long corpid){
		
		 Member member = new Member();
		 member.setHeadimgurl(headimgurl);
		 member.setNickname(nickname);
		 member.setCreateTime(new Date());
		 member.setStatus(StatusEnum.NORMAL.getValue());
		 memberDao.save(member);
		 
		 Membership membership = new Membership();
		 membership.setCorp(new Corporation(corpid));
		 membership.setMember(member);
		 membership.setOpenid(openid);
		 membershipDao.save(membership);
		 return membership;
	 }
 
	  
	@Override
	public Member getMember(Long id) throws ServiceException {

		return null;
	}

	@Override
	public void updatePassword(Member member) throws ServiceException {
		Member temp = memberDao.findOne(member.getId());
		temp.setPassword(member.getPassword());
		memberDao.save(temp);

	}

	@Override
	@Transactional(readOnly = false)
	public void updateMember(Member entity) throws ServiceException {
		// Membership userInfo = entity.getUserinfo();
		// userInfoDao.save(userInfo);
		memberDao.save(entity);

	}
 

	@Override
	public Member findMemberByUsername(String username) {

		return memberDao.findMemberByUsername(username);
	}

	@Override
	public Member regMember(Member member) {
		
		if(member!=null){ 
			memberDao.save(member);
		} 
		
		return member;
	}

}
